/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-05-25 16:02:44
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-10-24 14:51:18
 * @FilePath: /tea-garden-web/src/util/storeModules/smart-suggestion.js
 * @Description: 智能建议
 */
import { get } from '@/util/http.js'
import { ElMessage } from 'element-plus'
export default {
  state: {
    loading: {
      allAdvice: false,
      pickAdvice: false,
      weatherAdvice: false,
      diseaseAdvice: false,
      growthAdvice: false
    },
    allAdvice: [],
    pickAdvice: [],
    weatherAdvice: [],
    diseaseAdvice: [],
    growthAdvice: []
  },
  mutations: {
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    async getPickAdvice ({ commit }) {
      try {
        commit('setLoadingState', { key: 'allAdvice', value: true })
        commit('setCommonState', { key: 'allAdvice', value: [] })
        const { data, code } = await get({ url: '/Pick/IntelligentAdvice' })
        if (code === 1) {
          commit('setCommonState', { key: 'allAdvice', value: data })
        } else {
          ElMessage({ message: '采摘建议获取失败', type: 'warning' })
        }
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'allAdvice', value: false })
    },
    async getWeatherAdvice ({ commit }) {
      try {
        commit('setLoadingState', { key: 'allAdvice', value: true })
        commit('setCommonState', { key: 'allAdvice', value: [] })
        const { data, code } = await get({ url: '/Meteorology/IntelligentAdvice' })
        if (code === 1) {
          commit('setCommonState', { key: 'allAdvice', value: data })
        } else {
          ElMessage({ message: '天气建议获取失败', type: 'warning' })
        }
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'allAdvice', value: false })
    },
    async getDiseaseAdvice ({ commit }) {
      try {
        commit('setLoadingState', { key: 'allAdvice', value: true })
        commit('setCommonState', { key: 'allAdvice', value: [] })
        const { data, code } = await get({ url: '/Diseases/IntelligentAdvice' })
        if (code === 1) {
          commit('setCommonState', { key: 'allAdvice', value: data })
        } else {
          ElMessage({ message: '病虫害建议获取失败', type: 'warning' })
        }
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'allAdvice', value: false })
    },
    async getGrowthAdvice ({ commit }) {
      try {
        commit('setLoadingState', { key: 'allAdvice', value: true })
        commit('setCommonState', { key: 'allAdvice', value: [] })
        const { data, code } = await get({ url: '/Growing/IntelligentAdvice' })
        if (code === 1) {
          commit('setCommonState', { key: 'allAdvice', value: [...data.manure_data, ...data.water_data] })
        } else {
          ElMessage({ message: '生长建议获取失败', type: 'warning' })
        }
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'allAdvice', value: false })
    },
    getAllAdvice ({ commit }) {
      commit('setLoadingState', { key: 'allAdvice', value: true })
      const requestPromises = [
        get({ url: '/agri_suggest/list' })
      ]
      Promise.all(requestPromises)
        .then(responses => {
          let all = []
          // 这里可以对每个响应进行处理
          const data1 = responses
          console.log(responses)
          // const data2 = responses[1].data
          // const data3 = responses[2].data
          // const data4 = responses[3].data
          if (data1) {
            all = data1[0]
          } else {
            ElMessage({ message: '建议获取失败', type: 'warning' })
          }
          // if (data2) {
          //   all = [...all, ...data2]
          // } else {
          //   ElMessage({ message: '天气建议获取失败', type: 'warning' })
          // }
          // if (data3) {
          //   all = [...all, ...data3]
          // } else {
          //   ElMessage({ message: '病虫害建议获取失败', type: 'warning' })
          // }
          // if (data4) {
          //   all = [...all, ...data4.manure_data, ...data4.water_data]
          // } else {
          //   ElMessage({ message: '生长建议获取失败', type: 'warning' })
          // }
          commit('setCommonState', { key: 'allAdvice', value: all })
          commit('setLoadingState', { key: 'allAdvice', value: false })
        })
        .catch(error => {
        // 捕获任何一个请求发生的错误
          console.error('Error:', error)
          commit('setLoadingState', { key: 'allAdvice', value: false })
        })
    }
  }
}
